package org.porridge.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.porridge.po.Resource;
import org.porridge.util.SVNManager;

/**
 * 
 * 
 * <p>Title: findsvnfile   </p>
 * <p>Description:查询SVN数据</p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: svnkit</p>
 * <p>Create Date/Time: 2014年8月12日 下午8:23:27<p>
 * @author porridge
 * @version 1.0
 * @url www.prdblog.com
 *
 */
public class findsvnfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SVNManager svn=(SVNManager) request.getSession().getAttribute("svn");
		if(svn!=null){
			Object obj=request.getParameter("path");
			String path="";
			if(obj!=null && obj.toString().trim().length()>0)
				path=obj.toString();
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out=response.getWriter();
			
			if(svn!=null){
				Resource res = new Resource();
				res.setPath(path);
				try {
					List<Resource> rs = svn.getChildren(res);
					String json="{\"path\":\""+path+"\",\"size\":\""+rs.size()+"\",files:[";
					StringBuffer stb=new StringBuffer();
					for(Resource r:rs){//\"\":\"\"
						stb.append("{\"path\":\""+r.getPath()+"\",\"name\":\""+r.getName()+"\",\"isfile\":\""+r.getFile()+"\",\"svnversion\":\""+r.getSVNVersion()+"\"},");
					}
					if(stb.length()>0)
						json+=stb.substring(0,stb.length()-1);
					json+="]}";
					out.print(json);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
